
/*ajimber */
package vista.ParkingMantenimiento;

import java.util.ArrayList;
import java.util.List;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import modelo.entidades.Parking;

public class ParkingMantenimientoTableModel implements TableModel {

    private List<Parking> parkings = new ArrayList<Parking>();
    private List<TableModelListener> tableListeners = new ArrayList<TableModelListener>();
    //private List<ListDataListener> dataListeners = new ArrayList<ListDataListener>();
    private static ParkingMantenimientoTableModel model = null;

    private ParkingMantenimientoTableModel() {
    }

    public static ParkingMantenimientoTableModel create() {
        if (model == null) {
            model = new ParkingMantenimientoTableModel();
        }
        return model;
    }

    public List<Parking> getParkings() {
        return parkings;
    }

    public void setParkings(List<Parking> parkings) {
        this.parkings = parkings;
        fireContentsChanged();
    }

    protected void fireContentsChanged() {
        fireContentsChangedTableModel();
    }
    //--------------- MÉTODOS PROPIOS DE TableModel -------------------------

    public Parking getRow(int indexRow) {
        return parkings.get(indexRow);
    }

    @Override
    public int getRowCount() {
        return parkings.size();
    }

    @Override
    public int getColumnCount() {
        return 5;
    }

    @Override
    public String getColumnName(int columnIndex) {
        String name = null;
        switch (columnIndex) {
            case 0:
                name = "Nombre";
                break;
            case 1:
                name = "Plazas";
                break;
            case 2:
                name = "Libres";
                break;
            case 3:
                name = "Grupo";
                break;
            case 4:
                name = "Variable";
                break;

        }
        return name;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class; //Todos
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false; //No permito editar celdas
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Parking parking = parkings.get(rowIndex);
        String value = null;
        switch (columnIndex) {
            case 0:
                value = parking.getNombre();
                break;
            case 1:
                value = String.valueOf(parking.getPlazas());
                break;
            case 2:
                value = String.valueOf(parking.getPlazasLibres());
                break;
            case 3:
                value = parking.getGrupo();
                break;
            case 4:
                value = parking.getVariable();
                break;
        }
        return value;

    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void addTableModelListener(TableModelListener l) {
        tableListeners.add(l);
    }

    @Override
    public void removeTableModelListener(TableModelListener l) {
        tableListeners.remove(l);
    }

    protected void fireContentsChangedTableModel() {
        //TableModelEvent event = new TableModelEvent(this,0,getRowCount());
        TableModelEvent event = new TableModelEvent(this, 0, this.getRowCount() - 1, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT);
        for (TableModelListener listener : tableListeners) {
            listener.tableChanged(event);
        }
    }
}
